//
//  CustomerInfoView.m
//  mendianjieke
//
//  Created by 响当当 on 7/31/19.
//  Copyright © 2019 dongzheng. All rights reserved.
//

#import "CustomerInfoView.h"
#import "CustomerHeadView.h"
#import "CustomerInfoCell.h"
#import "AttackCell.h"

@interface CustomerInfoView()<UITableViewDelegate,UITableViewDataSource>
@property (nonatomic, strong)UIButton *customerButton;
@property (nonatomic, strong)UIButton *attackButton;
@property (nonatomic, strong)UIView *customerLineView;
@property (nonatomic, strong)UIView *attackLineView;

@property (nonatomic, strong)CustomerHeadView *customerHeadView;
@property (nonatomic, strong)UITableView *tableView;
@property (nonatomic, copy)NSArray *infoArr;

@property (nonatomic,strong) NSMutableArray *attackArr;
@property (nonatomic,assign) NSInteger index;

@property (nonatomic, strong)NSDictionary *customerDic;
@property (nonatomic, strong)NSDictionary *customerAttackDic;
@property (nonatomic, strong)CustomerInfoModel *model;
@property (nonatomic,strong) NSMutableArray *dataArray;
@property (nonatomic,strong) NSMutableArray *detailInfoArr;

@end

@implementation CustomerInfoView

- (NSDictionary *)customerDic
{
    if (!_customerDic)
    {
        _customerDic = [[NSDictionary alloc]init];
    }
    
    return _customerDic;
}
- (NSMutableArray *)dataArray {
    if (!_dataArray) {
        _dataArray = [[NSMutableArray alloc]init];
    }
    return _dataArray;
}

- (NSMutableArray *)detailInfoArr {
    if (!_detailInfoArr) {
        _detailInfoArr = [[NSMutableArray alloc]init];
    }
    return _detailInfoArr;
}

- (NSMutableArray *)attackArr {
    if (!_attackArr) {
        _attackArr = [[NSMutableArray alloc]init];
    }
    return _attackArr;
}

- (NSArray *)infoArr
{
    if (!_infoArr)
    {
        _infoArr = [[NSArray alloc]initWithObjects:self.model.gender.stringValue,self.model.age.stringValue,self.model.joinTime.stringValue,self.model.maxCost.stringValue,self.model.minCost.stringValue,self.model.avgCost.stringValue,self.model.buyingCycle.stringValue,self.model.lifeCycle,nil];
    }
    return _infoArr;
}

- (UIButton *)customerButton
{
    if (!_customerButton)
    {
        _customerButton = [Utilties creatButtonWithFrame:CGRectMake(0,0,187*kUIScaleBasePlus, 45*kUIScaleBasePlus)
                                            textAligment:UIControlContentHorizontalAlignmentCenter
                                                    text:@"顾客信息"
                                                textClor:LCHexColor(0x282828)
                                               buttonBGC:[UIColor whiteColor]
                                                textFont:15*kUIScaleBasePlus
                                                  target:self
                                                  method:@selector(segmentClick:)];
        [_customerButton.titleLabel setFont:[UIFont fontWithName:@"Helvetica-Bold" size:15*kUIScaleBasePlus]];
        _customerButton.tag = 0;
        
    }
    return _customerButton;
}

- (UIView*)customerLineView
{
    if (!_customerLineView)
    {
        _customerLineView = [[UIView alloc]initWithFrame:CGRectMake(62*kUIScaleBasePlus,35*kUIScaleBasePlus,60*kUIScaleBasePlus, 2*kUIScaleBasePlus)];
        _customerLineView.backgroundColor = LCHexColor(0xFF7A17);
    }
    return _customerLineView;
}

- (UIButton *)attackButton
{
    if (!_attackButton)
    {
        _attackButton = [Utilties creatButtonWithFrame:CGRectMake(CGRectGetMaxX(self.customerButton.frame),0,188*kUIScaleBasePlus, 45*kUIScaleBasePlus)
                                            textAligment:UIControlContentHorizontalAlignmentCenter
                                                    text:@"攻略"
                                                textClor:LCHexColor(0x999999)
                                               buttonBGC:[UIColor whiteColor]
                                                textFont:15*kUIScaleBasePlus
                                                  target:self
                                                method:@selector(segmentClick:)];
        _attackButton.tag = 1;
    }
    return _attackButton;
}

- (UIView*)attackLineView
{
    if (!_attackLineView)
    {
        _attackLineView = [[UIView alloc]initWithFrame:CGRectMake(264*kUIScaleBasePlus,35*kUIScaleBasePlus,32*kUIScaleBasePlus, 2*kUIScaleBasePlus)];
        _attackLineView.backgroundColor = LCHexColor(0xFF7A17);
        _attackLineView.hidden = YES;
    }
    return _attackLineView;
}


- (CustomerHeadView *)customerHeadView
{
    if (!_customerHeadView)
    {
        _customerHeadView = [[CustomerHeadView alloc]initWithFrame:CGRectMake(0,0,SCREEN_WIDTH,200*kUIScaleBasePlus)];
        _customerHeadView.backgroundColor = [UIColor whiteColor];
    }
    return _customerHeadView;
}


- (UITableView *)tableView
{
    if (!_tableView)
    {
        _tableView = [[UITableView alloc]initWithFrame:CGRectMake(0,50*kUIScaleBasePlus,SCREEN_WIDTH,SCREEN_HEIGHT-NAVIBAR_HEIGHT-50*kUIScaleBasePlus) style:UITableViewStyleGrouped];
        _tableView.delegate = self;
        _tableView.dataSource = self;
        _tableView.separatorStyle = 0;
        _tableView.backgroundColor = [UIColor whiteColor];
        _tableView.tableHeaderView = self.customerHeadView;
        _tableView.showsVerticalScrollIndicator = NO;

        MJRefreshNormalHeader *header = [MJRefreshNormalHeader headerWithRefreshingTarget:self refreshingAction:@selector(loadNewData)];
        
        // 设置文字
        [header setTitle:@"加载更多" forState:MJRefreshStateIdle];
        [header setTitle:@"下拉刷新" forState:MJRefreshStatePulling];
        [header setTitle:@"加载中" forState:MJRefreshStateRefreshing];
        
        // 设置字体
        header.stateLabel.font = [UIFont systemFontOfSize:15];
        header.lastUpdatedTimeLabel.font = [UIFont systemFontOfSize:12];
        
        // 设置颜色
        header.stateLabel.textColor = LCHexColor(0x989898);
        header.lastUpdatedTimeLabel.textColor = LCHexColor(0x989898);
        
        // 设置刷新控件
        self.tableView.mj_header = header;
        
        //上拉刷新
        MJRefreshAutoNormalFooter *footer = [MJRefreshAutoNormalFooter footerWithRefreshingTarget:self refreshingAction:@selector(loadNewFootData)];
        //设置上拉刷新属性
        [footer setTitle:@"加载更多" forState:MJRefreshStateIdle];
        [footer setTitle:@"加载中" forState:MJRefreshStateRefreshing];
        
        // 设置字体
        footer.stateLabel.font = [UIFont systemFontOfSize:17];
        
        // 设置颜色
        footer.stateLabel.textColor = LCHexColor(0x989898);
        
        //        _tableView.mj_footer = footer;
    }
    return _tableView;
}

- (instancetype)initWithFrame:(CGRect)frame
{
    self = [super initWithFrame:frame];
    if (self)
    {
        self.backgroundColor = [UIColor groupTableViewBackgroundColor];
        [self addSubview:self.customerButton];
        [self addSubview:self.attackButton];
        [self addSubview:self.customerLineView];
        [self addSubview:self.attackLineView];
        [self addSubview:self.tableView];
    }
    return self;
}

#pragma mark - delegate & dataSource
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView
{
    return 1;
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    if (self.index == 0)
    {
        return self.dataArray.count;
    }
    else
    {
        return self.attackArr.count;
    }
}

- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
{
    if (self.index == 0)
    {
        TableViewModel *model = self.dataArray[indexPath.row];
        return model.cellHeight;
    }
    else
    {
        
        AttackModel *model = self.attackArr[indexPath.row];
        return model.cellHeight;
    }
    
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    UITableViewCell *backcell = nil;
    
    if (self.index == 0)
    {
        CustomerInfoCell *cell = [tableView dequeueReusableCellWithIdentifier:@"CustomerInfoCell"];
        if (!cell)
        {
            cell = [[CustomerInfoCell alloc]initWithStyle:UITableViewCellStyleDefault reuseIdentifier:@"CustomerInfoCell"];
            cell.selectionStyle = 0;
        }
        
        cell.model = self.dataArray[indexPath.row];
        
        switch (self.detailInfoArr.count)
        {
            case 1:
            {
                TableViewDetailModel *model = [[TableViewDetailModel alloc]init];
                model = self.detailInfoArr[0];
                if (model.isHaveConsume.integerValue == 0)
                {
                    if (indexPath.row==4)
                    {
                        cell.detaileModel = model;
                    }
                }
                else
                {
                    if (indexPath.row==5)
                    {
                        cell.detaileModel = model;
                    }
                }
            }
                break;
            case 2:
            {
                
                if (indexPath.row == 4)
                {
                    TableViewDetailModel *model = [[TableViewDetailModel alloc]init];
                    model = self.detailInfoArr[0];
                    cell.detaileModel = model;
                }
                else if(indexPath.row == 5)
                {
                    TableViewDetailModel *model = [[TableViewDetailModel alloc]init];
                    model = self.detailInfoArr[1];
                    cell.detaileModel = model;
                }
            }
                break;
            default:
                break;
        }
        
        backcell = cell;
    }
    else
    {
        AttackCell *cell = [tableView dequeueReusableCellWithIdentifier:@"AttackCell"];
        if (!cell)
        {
            cell = [[AttackCell alloc]initWithStyle:UITableViewCellStyleDefault reuseIdentifier:@"AttackCell"];
            cell.selectionStyle = 0;
        }
        
        cell.model = self.attackArr[indexPath.row];
        backcell = cell;
    }
   
    
    return backcell;
}

#pragma mark - Method
- (void)loadNewData
{
    WeakSelf(self)
    dispatch_after(dispatch_time(DISPATCH_TIME_NOW, (int64_t)(1.5 * NSEC_PER_SEC)), dispatch_get_main_queue(), ^{
        [weakSelf.tableView.mj_header endRefreshing];
    });
}

- (void)loadNewFootData
{
    WeakSelf(self)
    dispatch_after(dispatch_time(DISPATCH_TIME_NOW, (int64_t)(1.5 * NSEC_PER_SEC)), dispatch_get_main_queue(), ^{
        
        [weakSelf.tableView.mj_footer endRefreshing];
        
    });
    
}

- (void)segmentClick:(UIButton *)sender
{
    self.index = sender.tag;
    if (sender.tag == 0)
    {
        self.customerLineView.hidden = NO;
        self.attackLineView.hidden = YES;
        
        [self.customerButton.titleLabel setFont:[UIFont fontWithName:@"Helvetica-Bold" size:15*kUIScaleBasePlus]];
        [self.attackButton.titleLabel setFont:[UIFont systemFontOfSize:15*kUIScaleBasePlus]];
        [self.customerButton setTitleColor:LCHexColor(0x000000) forState:UIControlStateNormal];
        [self.attackButton setTitleColor:LCHexColor(0x999999) forState:UIControlStateNormal];

    }
    else
    {
        
        self.customerLineView.hidden = YES;
        self.attackLineView.hidden = NO;
        
        [self.customerButton.titleLabel setFont:[UIFont systemFontOfSize:15*kUIScaleBasePlus]];
        [self.attackButton.titleLabel setFont:[UIFont fontWithName:@"Helvetica-Bold" size:15*kUIScaleBasePlus]];
        [self.customerButton setTitleColor:LCHexColor(0x999999) forState:UIControlStateNormal];
        [self.attackButton setTitleColor:LCHexColor(0x000000) forState:UIControlStateNormal];
    }
    
    if (self.index == 0)
    {
        self.tableView.tableHeaderView = self.customerHeadView;
    }
    else
    {
        self.tableView.tableHeaderView = [UIView new];
        self.tableView.tableHeaderView.height = 0.1;
    }
    
    [self.tableView reloadData];
}


- (void)getInfo:(NSDictionary *)dic
{
    self.customerDic = dic;
    self.model = [[CustomerInfoModel alloc]initWithDic:dic[@"summary"]];
    
    NSMutableArray *dataArr = [[NSMutableArray alloc]init];
    NSMutableArray *infoArr = [[NSMutableArray alloc]init];

    NSArray *timeAxisArr = self.customerDic[@"timeAxis"];
    bool isHaveConsumeOne = NO;
    bool isHaveConsume = NO;

    for (int i=0; i<timeAxisArr.count; i++)
    {
        
        NSString *str = timeAxisArr[i];

        switch (i)
        {
            case 0:
            {
                [dataArr addObject:[NSString stringWithFormat:@"%@\n\n%@",[str substringToIndex:2],[str substringFromIndex:3]]];
            }
                break;
            case 1:
            {
                [dataArr addObject:[NSString stringWithFormat:@"%@\n\n%@",[str substringToIndex:6],[str substringFromIndex:7]]];

            }
                break;
            case 2:
            {
                 [dataArr addObject:[NSString stringWithFormat:@"%@\n\n%@",[str substringToIndex:7],[str substringFromIndex:8]]];
            }
                break;
            case 3:
            {
                [dataArr addObject:[NSString stringWithFormat:@"%@\n\n%@",[str substringToIndex:8],[str substringFromIndex:9]]];

            }
                break;
            case 4:
            {
                NSArray *customerTwoArr = [str componentsSeparatedByString:@"/"];
                NSString *time = customerTwoArr[2];
                NSString *inShop = customerTwoArr[1];
                NSString *outShop = customerTwoArr[3];
                NSString *detailStr = customerTwoArr[4];
                
                   if (customerTwoArr.count > 5)
                   {
                       isHaveConsume = YES;
                   }
                   if (isHaveConsume)
                   {
                       NSString *strss = [NSString stringWithFormat:@"%@,%@,%@,%@,%@",customerTwoArr[0],inShop,time,outShop,detailStr];
                       [dataArr addObject:strss];
                       [infoArr addObject:customerTwoArr];
                   }
                   else
                   {
                       NSString *strss = [NSString stringWithFormat:@"%@,%@,%@,%@,%@",customerTwoArr[0],inShop,time,outShop,detailStr];
                       [dataArr addObject:strss];
                   }
                
            }
                break;
            case 5:
            {
                NSArray *customerOneArr = [str componentsSeparatedByString:@"/"];
                   NSString *timeOne = customerOneArr[2];
                   NSString *inShopOne = customerOneArr[1];
                   NSString *outShopOne = customerOneArr[3];
                   NSString *detailStrOne = customerOneArr[4];
                   if (customerOneArr.count > 5)
                   {
                       isHaveConsumeOne = YES;
                   }
                   if (isHaveConsumeOne)
                   {
                       NSString *strss = [NSString stringWithFormat:@"%@,%@,%@,%@,%@",customerOneArr[0],inShopOne,timeOne,outShopOne,detailStrOne];
                       [dataArr addObject:strss];
                       [infoArr addObject:customerOneArr];
                   }
                   else
                   {
                       NSString *strss = [NSString stringWithFormat:@"%@,%@,%@,%@,%@",customerOneArr[0],inShopOne,timeOne,outShopOne,detailStrOne];
                       [dataArr addObject:strss];
                   }
                   
            }
                break;
            default:
                break;
        }
        
    }

    
    for (int i=0; i < infoArr.count; i++)
    {
        TableViewDetailModel *model = [[TableViewDetailModel alloc]init];
        NSArray *arr = infoArr[i];
        
        NSMutableArray *arrs = [[NSMutableArray alloc]init];
        
        for (int y = 5; y<arr.count; y++)
        {
            [arrs addObject:arr[y]];
        }
        
        if (isHaveConsume)
        {
            model.isHaveConsume = @"0";
            model.detailArr = arrs;
        }
        
        if (isHaveConsumeOne)
        {
            model.isHaveConsume = @"1";
            model.detailArr = arrs;
        }
       
        [self.detailInfoArr addObject:model];
    }
        
    for (int i = 0; i < dataArr.count; i ++)
    {
        TableViewModel *model = [[TableViewModel alloc]init];
        
        model.num = [NSString stringWithFormat:@"%d",i];
        
        model.text = dataArr[i];
        model.cellHeight = [CustomerInfoCell heightWithModel:model];
        
        if (i > 3 && i<=5 )
        {
            NSString *str = dataArr[i];
            
            NSArray *arr = [str componentsSeparatedByString:@","];
            
            model.context = arr[0];
            model.inShop = arr[1];
            model.time = arr[2];
            model.outShop = arr[3];
            model.detail = arr[4];
            
        }
        
        if (dataArr.count-1 == i)
        {
            model.lastnum = @"1";
        }
        else
        {
            model.lastnum = @"-1";
        }
        
        [self.dataArray addObject:model];
    }
    
    [self.customerHeadView getInfo:self.infoArr headImage:dic[@"photo"]];
    [self.tableView reloadData];
}

- (void)customerAttack:(NSDictionary *)attackDic
{
    self.customerAttackDic = attackDic;
    
    NSArray *customerArr = attackDic[@"detail"];
    
    NSMutableArray *arr = [[NSMutableArray alloc]init];
    
    [arr addObject:@{@"index":@"所在价值链",@"title":[NSString stringWithFormat:@"%@",attackDic[@"memberGroup"]],@"subTitle":@""}];
    
    [arr addObject:@{@"index":@"客户类型",@"title":[NSString stringWithFormat:@"%@",attackDic[@"type"]],@"subTitle":@""}];
    
    for (int i=0; i<customerArr.count; i++)
    {
        NSDictionary *dic = [NSDictionary dictionaryWithDictionary:customerArr[i]];
        
        [arr addObject:@{@"index":dic[@"type"],@"title":[NSString stringWithFormat:@"%@",dic[@"name"]],@"subTitle":[NSString stringWithFormat:@"%@",dic[@"content"]]}];
    }
    
    for (int i = 0; i < arr.count; i ++) {
        AttackModel *model = [[AttackModel alloc]init];
        NSDictionary *dic = arr[i];
        model.index = dic[@"index"];
        model.title = dic[@"title"];
        model.subtitle = dic[@"subTitle"];
        model.num = [NSString stringWithFormat:@"%d",i];
        model.cellHeight = [AttackCell heightWithModel:model];
        [self.attackArr addObject:model];
    }
}
@end
